package com.netclass.meethealth.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.netclass.meethealth.R;
import com.netclass.meethealth.adapter.PartAdapter;
import com.netclass.meethealth.callback.BoolCallBack;
import com.netclass.meethealth.callback.SportCallBack;
import com.netclass.meethealth.model.Sport;
import com.netclass.meethealth.srv.SportService;
import com.netclass.meethealth.srv.impl.SportSrvImpl;
import com.netclass.meethealth.tools.PreferenceManager;
import com.netclass.meethealth.view.DragListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 组团运动的activity
 * <p/>
 * Created by luodong on 15/12/13.
 */
public class PartActivity extends Activity {

    //控件
    private DragListView listView;

    //适配器
    private PartAdapter partAdapter;

    //服务
    private SportService sportService;

    //    页码
    private int page = 1;

    //    用户的坐标
    private double lat = 0;
    private double lon = 0;

    //    信息List
    private List<Sport> sports = new ArrayList<>();

    //我的信息
    private String mPhone;
    private String mName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part);
        listView = (DragListView) findViewById(R.id.list_part);

        sportService = new SportSrvImpl();

        //测试
        PreferenceManager.set(this, "phone", "123456");
        PreferenceManager.set(this, "name", "测试");

        mName = PreferenceManager.getString(this, "name");
        mPhone = PreferenceManager.getString(this, "phone");

        initListeners();
        loadLastedData();

    }

    /**
     * 初始化监听器
     */
    private void initListeners() {
        listView.setOnRefreshListener(new DragListView.OnRefreshLoadingMoreListener() {
            @Override
            public void onRefresh() {
                //        下拉刷新
                loadLastedData();
            }

            @Override
            public void onLoadMore() {
//                上拉加载更多
                loadMoreData();
            }
        }, 100);

//        点击列表项
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()

                                        {
                                            @Override
                                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                Intent intent = new Intent();
                                                Bundle bundle = new Bundle();
                                                bundle.putSerializable("sport", sports.get(position - 1));
                                                intent.putExtras(bundle);
                                                intent.setClass(PartActivity.this, PartDetailActivity.class);
                                                startActivity(intent);
                                            }
                                        }

        );

    }

    /**
     * 加载最新的数据
     */
    private void loadLastedData() {
        page = 1;
        sportService.listSport(page, mPhone, lat, lon, new SportCallBack() {
            @Override
            public void onSuccess(Sport sport) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                    }
                });
            }

            @Override
            public void onGetList(final List<Sport> sportList) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        sports = sportList;
                        if (partAdapter == null) {
                            partAdapter = new PartAdapter(PartActivity.this, sports);
                            partAdapter.setOnItemButtonClickListener(new PartAdapter.OnItemButtonClickListener() {
                                @Override
                                public void click(int position, boolean join) {
                                    joinOrCancel(sports.get(position).getId(), join);
                                }
                            });
                            listView.setAdapter(partAdapter);
                        } else
                            partAdapter.refresh(sports);
                        listView.onRefreshComplete();
                        Toast.makeText(PartActivity.this, "获取到" + sportList.size() + "条数据", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onFailed(final String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                        Toast.makeText(PartActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void onNull() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listView.onRefreshComplete();
                        Toast.makeText(PartActivity.this, "没有更多啦", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    /**
     * 加载更多的数据
     */
    private void loadMoreData() {
        sportService.listSport(++page, mPhone, lat, lon, new SportCallBack() {
                    @Override
                    public void onSuccess(Sport sport) {
                        page--;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                listView.onLoadMoreComplete(true);
                            }
                        });
                    }

                    @Override
                    public void onGetList(final List<Sport> sportList) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                sports.addAll(sportList);
                                if (partAdapter == null) {
                                    partAdapter = new PartAdapter(PartActivity.this, sports);
                                    partAdapter.setOnItemButtonClickListener(new PartAdapter.OnItemButtonClickListener() {
                                        @Override
                                        public void click(int position, boolean join) {
                                            joinOrCancel(sports.get(position).getId(), join);
                                        }
                                    });
                                    listView.setAdapter(partAdapter);
                                } else
                                    partAdapter.refresh(sports);
                                if (sports.size() < 20) {
                                    listView.onLoadMoreComplete(true);
                                } else {
                                    listView.onLoadMoreComplete(false);
                                }
                                Toast.makeText(PartActivity.this, "获取到" + sportList.size() + "条数据", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onFailed(final String message) {
                        page--;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                listView.onLoadMoreComplete(true);
                                Toast.makeText(PartActivity.this, message, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onNull() {
                        page--;

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                listView.onLoadMoreComplete(true);
                                Toast.makeText(PartActivity.this, "没有更多啦", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }

        );
    }

    /**
     * 参加或取消请求
     *
     * @param sId
     * @param join
     */
    private void joinOrCancel(int sId, boolean join) {
        if (join) {
            //加入
            sportService.applySport(sId, mName, mPhone, new BoolCallBack() {
                @Override
                public void onYes() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PartActivity.this, "成功啦", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onNo() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PartActivity.this, "失败啦", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        } else {
            //退出
            sportService.deleteApply(sId, "123456", new BoolCallBack() {
                @Override
                public void onYes() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PartActivity.this, "成功啦", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onNo() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PartActivity.this, "失败啦", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }
    }
}
